package com.test.clyq.action;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.test.clyq.entity.Category;
import com.test.clyq.entity.DataPermission;
import com.test.clyq.entity.Users;
import com.test.clyq.service.CategoryService;
import com.test.clyq.service.DataPermissionService;
import com.test.clyq.service.UserService;
import com.test.json.JsonTree;
import com.test.util.FormatExceptionStack;
import com.test.util.ObjectUtils;
import com.test.util.StringUtils;
import com.test.webapp.action.BaseAction;

/**
 * DatePermissionAction. <br>
 * 数据权限管理（栏目权限）.
 * <p>
 * Copyright: Copyright (c) 2010-4-18 下午04:51:40
 * <p>
 * Company: 北京宽连十方数字技术有限公司
 * <p>
 * Author: liuxl@c-platform.com
 * <p>
 * Version: 1.0
 * <p>
 */
public class DataPermissionAction extends BaseAction {

	/**
	 * 为用户添加/修改权限
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public ActionForward optUserDataPermission(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	        throws IOException {
		this.log.info("操作用户权限start");
		UserService userService = (UserService) getBean("userService");
		DataPermissionService dataPermissionService = (DataPermissionService) getBean("dataPermissionService");
		try {

			String suserId = getParameter(request, "userId");
			// 传递进来的id不为空 并且为数字
			if (!StringUtils.isEmpty(suserId) && StringUtils.isNumeric(suserId)) {
				Users user = userService.getUsersById(Long.valueOf(suserId));
				String dataPermissionStr = getParameter(request, "permission");
				String[] dataPermissionArray = StringUtils.split(dataPermissionStr, ",");
				Set<DataPermission> dataPermissionSet = dataPermissionService.getDatePermissionByUserIdAndCategoryId(null, user.getId());
				if (dataPermissionSet != null && !dataPermissionSet.isEmpty()) {
					for (DataPermission permission : dataPermissionSet) {
						dataPermissionService.delDataPermissionById(permission.getDataPermissionId(), user);
					}
				}
				for (String permission : dataPermissionArray) {
					DataPermission perm = new DataPermission();
					perm.setDatePermissionCategoryId(Long.valueOf(permission));
					perm.setDatePermissionUserId(user.getId());
					dataPermissionService.addDataPermission(perm, user);
				}
			}
			print(response, true);

			this.log.debug("操作用户权限成功");
			this.log.info("操作用户权限end");
		}
		catch (Exception e) {
			this.log.error("操作用户权限:失败 " + FormatExceptionStack.formatException(e));
			print(response, false);
		}
		return null;
	}

	/**
	 * 展示用户权限
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public ActionForward showUserDataPermission(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	        throws IOException {
		this.log.info("展示用户权限start");
		UserService userService = (UserService) getBean("userService");
		CategoryService categoryService = (CategoryService) getBean("categoryService");
		DataPermissionService dataPermissionService = (DataPermissionService) getBean("dataPermissionService");
		try {
			String suserId = getParameter(request, "userId");
			// 传递进来的id不为空 并且为数字
			if (!StringUtils.isEmpty(suserId) && StringUtils.isNumeric(suserId)) {
				Users user = userService.getUsersById(Long.valueOf(suserId));
				if (!ObjectUtils.isNull(user)) {
					List<Category> categoryList = categoryService.getCategorys();
					Set<Category> permissionSet = new HashSet<Category>();
					Set<DataPermission> dataPermissionSet = dataPermissionService.getDatePermissionByUserIdAndCategoryId(null, user.getId());
					if (dataPermissionSet != null && !dataPermissionSet.isEmpty()) {
						for (DataPermission permission : dataPermissionSet) {
							Category category = categoryService.getCategoryById(permission.getDatePermissionCategoryId());
							if (!ObjectUtils.isNull(category)) {
								permissionSet.add(category);
							}
						}
					}
					String str = JsonTree.getJsonStringByList(categoryList, "categoryName", "categoryParentId", permissionSet);
					print(response, str, null, null);
				}
			}
			this.log.debug("展示用户权限成功");
			this.log.info("展示用户权限end");
		}
		catch (Exception e) {
			this.log.error("展示用户权限:失败 " + FormatExceptionStack.formatException(e));
			print(response, false);
		}
		return null;
	}
}
